
package com.ifcar.app.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ifcar.app.activity.MyApplication;
import com.ifcar.app.activity.NewsPage;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.util.BaseImageDownloader;
import com.ifcar.app.util.PicUtil;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.util.Tools;
import com.ifcar.app.activity.R;

public class VoteWithTabAdapter extends HeadlineAdapter {
	
	private Context mContext;
    private LayoutInflater mInflater;
    private ThemeSettingsHelper mThemeSettingsHelper;
    private BaseImageDownloader mImageDownloader;
    
    public VoteWithTabAdapter(Context context, List<Map<String, Object>> list) {
        super(context, list);
        mInflater = LayoutInflater.from(context);
        mThemeSettingsHelper = ThemeSettingsHelper.getThemeSettingsHelper(context);
        mContext = context;
        
        int w = Tools.getScreenWidth(mContext);
        int h = Tools.getCutHeadPicHeight(w);
        mImageDownloader = new BaseImageDownloader(mContext, ((BitmapDrawable)mContext.getResources().getDrawable(R.drawable.vote_default)).getBitmap()
		        , w, h, "Y");
    }

    @Override
    public int getCount() {
        return getRealCount();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewparent) {
    	position = getRealPosition(position);
        if (getRealCount() == 0) {
            return null;
        }
        
        if (convertView == null) {
        	ViewHolder holderItem = new ViewHolder();
            convertView = mInflater.inflate(R.layout.vote_with_tab_item, null);
            
            holderItem.mImageView = (ImageView) convertView.findViewById(R.id.vote_image_item);
            holderItem.mVoteDigest = (TextView) convertView.findViewById(R.id.vote_digest);
            holderItem.mVoteQuestion = (TextView) convertView.findViewById(R.id.vote_question);
            holderItem.mVoteDate = (TextView) convertView.findViewById(R.id.vote_date);
            holderItem.mVoteFlag = (TextView) convertView.findViewById(R.id.vote_is_can);
            
            int w = Tools.getScreenWidth(mContext);
            int h = Tools.getCutHeadPicHeight(w);
            
            holderItem.mImageView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,h));
            
            convertView.setTag(holderItem);
            
        }
        
        ViewHolder holder = (ViewHolder) convertView.getTag();
        final Map<String, Object> map = (Map<String, Object>) getItem(position);
        String imgsrc = "" + map.get(GetColumnsInfo.IMGSRC_TAG);
        imgsrc = PicUtil.getHeandBitMapURL(imgsrc,mContext);
        
    	mImageDownloader.setThread(Thread.currentThread());
		mImageDownloader.download(imgsrc, holder.mImageView);
		
		String voteId = "" + map.get(StringUtil.VOTE_ID);
    	String voteDigest = "" + map.get(StringUtil.VOTE_DIGEST);
    	String voteQuestion = "" + map.get(StringUtil.VOTE_QUESTION);
    	String voteDate = "" + map.get(StringUtil.VOTE_DATE);
    	
    	holder.mVoteDigest.setText(voteDigest);
    	holder.mVoteQuestion.setText(voteQuestion);
    	holder.mVoteDate.setText(voteDate);
		
    	MyApplication app = (MyApplication)mContext.getApplicationContext();
    	SQLiteDatabase db = app.getSQLiteDatabase();
		Cursor cur = db.query(NewsSQLiteOpenHelper.VOTE_TABLE, new String[]{NewsSQLiteOpenHelper.VOTE_ID},
				NewsSQLiteOpenHelper.VOTE_ID + "=" + voteId, null, null, null, NewsSQLiteOpenHelper.VOTE_AUTO_ID + " desc");

		int flag = cur.getCount();
		cur.close();
		
		if(0 == position){
			if(flag>0){
				holder.mVoteFlag.setText(mContext.getResources().getString(R.string.vote_already));
			}else{
				holder.mVoteFlag.setText(mContext.getResources().getString(R.string.vote_ing));
			}
		}else{
			if(flag>0){
				holder.mVoteFlag.setText(mContext.getResources().getString(R.string.vote_already));
			}else{
				holder.mVoteFlag.setText(mContext.getResources().getString(R.string.vote_missing));
			}
		}
    	
		holder.mImageView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String docid = (String)map.get(StringUtil.VOTE_DOCID);
		        if(null!=docid && !"".equals(docid)){
		            Intent itemintent = new Intent(mContext, NewsPage.class);
		            Bundle b = new Bundle();
		            b.putString(GetColumnsInfo.DOCID_TAG, docid);
		            b.putString(GetColumnsInfo.TID_TAG, "");
	                b.putString("position", "0");
	                b.putBoolean(GetColumnsInfo.HEADLINE_TAG, false);
	                b.putBoolean("isCollect", true);
		            itemintent.putExtras(b);
		            mContext.startActivity(itemintent);
		        }
			}
		});
		setTheme(holder);
        return convertView;
    }
    
    
    class ViewHolder{
    	ImageView mImageView;
    	TextView mVoteDigest;
    	TextView mVoteQuestion;
    	TextView mVoteFlag;
    	TextView mVoteDate;
    }
    
    private void setTheme(ViewHolder holder){
    	mThemeSettingsHelper.setTextViewColor(mContext, holder.mVoteDigest, R.color.top_text_color);
    	mThemeSettingsHelper.setViewBackgroud(mContext, holder.mVoteDigest, R.drawable.top_text_background);
    	mThemeSettingsHelper.setTextViewColor(mContext, holder.mVoteDate, R.color.vote_desc_color);
    	mThemeSettingsHelper.setTextViewColor(mContext, holder.mVoteFlag, R.color.vote_desc_color);
    	mThemeSettingsHelper.setTextViewColor(mContext, holder.mVoteQuestion, R.color.vote_title_color);
    }
}
